<template>
  <div class="app-header-sticky" :class="{ show: y > 80 }">
    <div class="container">
      <RouterLink class="logo" to="/" />
      <AppHeaderNav />
      <div class="right">
        <RouterLink to="/">品牌</RouterLink>
        <RouterLink to="/">专题</RouterLink>
      </div>
    </div>
  </div>
</template>

<script>
import AppHeaderNav from './app-header-nav'
// import { onMounted, ref, onBeforeUnmount } from 'vue'
import { useWindowScroll } from '@vueuse/core'
export default {
  name: 'AppHeaderSticky',
  components: { AppHeaderNav },
  setup () {
    /*
1:默认不显示
2:滚动条滚动到一定间距才显示
   a:通过监听滚动事件  window.addEventListener('scroll')
   b:在滚动事件中实时获取scrollTop    document.documentElement.scrollTop ||  document.body.scrollTop || window.pageYOffset
   c:根据滚动的值判断是否显示吸顶组件
*/
    // 滚动事件
    // const isShow = ref(false)
    // 传统写法
    // function scrollEvent () {
    //   const _top =
    //     document.documentElement.scrollTop ||
    //     document.body.scrollTop ||
    //     window.pageYOffset
    //   if (_top > 80) {
    //     isShow.value = true
    //   } else {
    //     isShow.value = false
    //   }
    // }
    // onMounted(() => {
    //   window.addEventListener('scroll', scrollEvent)
    // })
    // onBeforeUnmount(() => {
    //   window.removeEventListener('scroll', scrollEvent)
    // })
    // 插件写法
    const { y } = useWindowScroll()
    return { y }
  }
}
</script>

<style scoped lang="less">
.app-header-sticky {
  width: 100%;
  height: 80px;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  background-color: #fff;
  border-bottom: 1px solid #e4e4e4;
  .container {
    display: flex;
    align-items: center;
  }
  .logo {
    width: 200px;
    height: 80px;
    background: url(../assets/images/logo.png) no-repeat right 2px;
    background-size: 160px auto;
  }
  .right {
    width: 220px;
    display: flex;
    text-align: center;
    padding-left: 40px;
    border-left: 2px solid @xtxColor;
    a {
      width: 38px;
      margin-right: 40px;
      font-size: 16px;
      line-height: 1;
      &:hover {
        color: @xtxColor;
      }
    }
  }
  opacity: 0;
  transform: translateY(-100%);
  &.show {
    transition: all 0.5s;
    opacity: 1;
    transform: none;
  }
}
</style>
